package cn.explink.dop.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.explink.dop.dao.AreaDao;
import cn.explink.dop.dao.OrderDao;
import cn.explink.dop.entity.vo.AreaVO;
import cn.explink.dop.entity.vo.OrderVO;
import cn.explink.dop.gis.GeoPoint;
import cn.explink.dop.gis.GeoUtility;
import cn.explink.dop.service.AreaService;
import cn.explink.eap.core.common.service.impl.CommonServiceImpl;

/**
 * 业务层
 *
 * @author firefly 2015-2-5 14:57
 *
 */
@Service
public class AreaServiceImpl extends CommonServiceImpl implements AreaService {

	@Autowired
	private AreaDao areaDao;

	@Autowired
	private OrderDao orderDao;

	@Override
	public String add(final AreaVO areaVO) {
		return (String) this.areaDao.save(areaVO);
	}

	@Override
	public void modify(final AreaVO areaVO) {
		this.areaDao.modify(areaVO);
	}

	@Override
	public List<AreaVO> getListData(final AreaVO areaVO) {
		return this.areaDao.finds(areaVO);
	}

	@Override
	public Map<String, Object> getListDataByPage(final AreaVO areaVO, final int pageNum, final int pageSize) {
		return this.areaDao.findByPage(areaVO, pageNum, pageSize);
	}

	/**
	 *
	 * @Title: findVoByPage
	 * @Description: 分页查询实体
	 * @author 孟凡岭
	 * @param @param areaVO
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @return
	 * @throws
	 */
	@Override
	public Map<String, Object> findVoByPage(AreaVO areaVO, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		return this.areaDao.findVoByPage(areaVO, pageNum, pageSize);
	}

	@Override
	public int deleteData(final List<AreaVO> areaVOList) {
		return this.areaDao.delete(areaVOList);
	}

	@Override
	public void modifyByUid(AreaVO areaVO) {
		this.areaDao.modifyByUid(areaVO);
	}

	@Override
	public AreaVO queryById(String id) {
		AreaVO areaVO = this.areaDao.queryById(id);
		return areaVO;
	}

	@Override
	public List<OrderVO> getOrderVOListByArea(AreaVO areaVOParam, List<Integer> statusList) {
		List<OrderVO> belongAreaOrderVOList = new ArrayList<OrderVO>();

		List<OrderVO> orderVOList = this.orderDao.getByStatusAndDate(null, statusList, null, null);

		// 判断订单是否在区域内

		for (OrderVO orderVO : orderVOList) {
			String position = orderVO.getCustPosition();
			if ((position == null) || (position == "")) {
				belongAreaOrderVOList.add(orderVO);
			} else if (this.isOrderInPolygon(position, areaVOParam.getBaseCoordinate())) {
				belongAreaOrderVOList.add(orderVO);
			}
		}

		// ori
		// for (OrderVO orderVO : orderVOList) {
		// List<AreaVO> areaVOList =
		// this.areaDao.getByAddress(orderVO.getCustAddress());
		// for (AreaVO areaVO : areaVOList) {
		// if (areaVOParam.getId().equals(areaVO.getId())) {
		// belongAreaOrderVOList.add(orderVO);
		// break;
		// }
		// }
		// }
		return belongAreaOrderVOList;
	}

	@Override
	public List<OrderVO> getOrderVOListByIdAndArea(AreaVO areaVOParam, List<Integer> statusList, String storeId) {
		List<OrderVO> belongAreaOrderVOList = new ArrayList<OrderVO>();				
		List<OrderVO> orderVOList = this.orderDao.getByStatusAndDate(null, statusList, null, null);
		
		//获取全部区域的list
		List<AreaVO> areaList= this.areaDao.finds(new AreaVO());
					
		for (OrderVO orderVO : orderVOList) {
			String position = orderVO.getCustPosition();
			String id = orderVO.getStorefrontEntity().getId();
			if ((!id.equals(storeId)) && (storeId != null)) {
				continue;
			}

			if (StringUtils.isBlank(position) || (areaVOParam == null)) {
				if (areaVOParam == null) {
					//List<AreaVO> areaVOList = this.areaDao.getByCoordinate(position);
					List<AreaVO> areaVOList = this.areaDao.getByCoordinateXiugai(position,areaList);
					if ((areaVOList != null) && (areaVOList.size() > 0)) {
						orderVO.setRegionID(areaVOList.get(0).getId());
						orderVO.setRegionName(areaVOList.get(0).getBaseAreaName());
					}
				} else {
					orderVO.setRegionID(areaVOParam.getId());
					orderVO.setRegionName(areaVOParam.getBaseAreaName());
				}
				belongAreaOrderVOList.add(orderVO);
			} else if (this.isOrderInPolygon(position, areaVOParam.getBaseCoordinate())) {
				orderVO.setRegionID(areaVOParam.getId());
				orderVO.setRegionName(areaVOParam.getBaseAreaName());
				belongAreaOrderVOList.add(orderVO);
			}
		}
		return belongAreaOrderVOList;
	}

	private Boolean isOrderInPolygon(String orderPosition, String regionPaths) {

		// TODO 格式可能变化
		regionPaths = regionPaths.substring(regionPaths.indexOf("path") + 6, regionPaths.indexOf("}]") + 2);
		if (regionPaths == null) {
			// 区域配送范围获取失败
			return false;
		}
		@SuppressWarnings("unchecked")
		List<GeoPoint> pts = (List<GeoPoint>) JSONArray.toCollection(JSONArray.fromObject(regionPaths), GeoPoint.class);
		if (pts == null) // 站点的配送区域坐标查询失败
		{
			return false;
		}

		GeoPoint position = (GeoPoint) JSONObject.toBean(JSONObject.fromObject(orderPosition), GeoPoint.class);

		// 空间检索
		GeoPoint[] polygon = new GeoPoint[pts.size()];
		return GeoUtility.isInPolygon(position, pts.toArray(polygon));

	}

	/**
	 *
	 * @Title: unEnabled
	 * @Description: 将启用状态改为停用
	 * @author 孟凡岭
	 * @param @param areaIdList
	 * @param @return
	 * @return boolean 返回类型
	 * @throws
	 */
	@Override
	public boolean unEnabled(List<String> areaIdList) {
		// TODO Auto-generated method stub
		return this.areaDao.unEnabled(areaIdList);
	}

	/**
	 *
	 * @Title: enabled
	 * @Description: 将停用状态改为启用
	 * @author 孟凡岭
	 * @param @param areaIdList
	 * @param @return
	 * @throws
	 */
	@Override
	public boolean enabled(List<String> areaIdList) {
		// TODO Auto-generated method stub
		return this.areaDao.enabled(areaIdList);
	}

}
